package com.teach;

import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;

public class PropertiesTest {

	public static void main(String[] args) {
		Properties p = new Properties();// Map<String,String>
		// 设置属性
		p.setProperty("name1", "value1");
		p.setProperty("name11", "value11");
//		p.put(key, value);//不建议使用
		System.out.println(p);
		// 获取属性
		String v = p.getProperty("name1");
		System.out.println("name1=" + v);
		System.out.println("p.getProperty(\"name2\")=" + p.getProperty("name2"));
		System.out.println("p.getProperty(\"name2\",\"默认值\")=" + p.getProperty("name2", "默认值"));
		{
			// 遍历属性
			System.out.println("------------遍历属性");
			Enumeration<?> names = p.propertyNames();
			while (names.hasMoreElements()) {
				String name = (String) names.nextElement();
				System.out.println(name + "=" + p.getProperty(name));
			}
		}
		{
			System.out.println("------------遍历属性");
			Set<String> names = p.stringPropertyNames();
			for(String name : names) {
				System.out.println(name + "=" + p.getProperty(name));
			}
		}
	}
}
